﻿using ProtoImaginer.ProtoProject;
/* 
    Copyright (c) 2011 Microsoft Corporation.  All rights reserved.
    Use of this sample source code is subject to the terms of the Microsoft license 
    agreement under which you licensed this sample source code and is provided AS-IS.
    If you did not accept the terms of the license agreement, you are not authorized 
    to use this sample source code.  For the terms of the license, please see the tr
    license agreement between you and Microsoft.
  
    To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604 
  
*/
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace ProtoImaginer.Models
{

    public class ProtoDataContext : DataContext
    {
        public static string DBConnectionString = "Data Source=isostore:/protoDataContext.sdf";

        // Pass the connection String to the base class.
        public ProtoDataContext(String connectionString)
            : base(connectionString) { }

        // Specify a table for the Projects.
        public Table<Project> Projects
        {
            get
            {
                return this.GetTable<Project>();
            }
        }

        // Specify a table for the Pages.
        public Table<Page> Pages
        {
            get
            {
                return this.GetTable<Page>();
            }
        }

        //Specify a table for the Controls.
        public Table<Control> Controls;
        //{
        //    get
        //    {
        //        return this.GetTable<Control>();
        //    }
        //}

        //Specify a table for the Images.
        public Table<Image> Images
        {
            get
            {
                return this.GetTable<Image>();
            }
        }

        ////Unused relation declaration
        ////Specify an association table for "A page contains up to 7 images."
        //public Table<Page_Contain_Image> PageImageList;

        ////Specify an association table for "An image contains a N controls."
        //public Table<Image_Contain_Control> ImageControlList;

        #region DB interfaces
        //control related interface
        public static IList<Control> LoadControlsByImageId(String imageId)
        {
            using (ProtoDataContext controlDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Control> control = from a in controlDB.Controls
                                              where a.ControlResidingImageId == imageId
                                              select a;
                return control.ToList();
            }
        }

        public static Control LoadControlByPK(String ControlId)
        {
            using (ProtoDataContext controlDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Control> control = from a in controlDB.Controls
                                              where a.ControlId == ControlId
                                              select a;
                return control.FirstOrDefault();
            }
        }

        public static bool SaveControl(Control c)
        {
            try
            {
                DeleteControl(c);
                AddControl(c);
            }
            catch
            {
                DeleteControl(c);
                AddControl(c);
                return false;
            }
            return true;
        }

        public static bool AddControl(Control c)
        {
            using (ProtoDataContext controlDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {

                controlDB.Controls.InsertOnSubmit(c);

                try
                {
                    controlDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    controlDB.SubmitChanges();
                }

                return true;
            }
        }

        //Assuming we delete the record from db directly rather than set the status of control to be deleted
        public static bool DeleteControl(Control c)
        {
            using (ProtoDataContext controlDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Control> control = from a in controlDB.Controls
                                              where a.ControlId == c.ControlId
                                              select a;

                foreach (var c1 in control)
                {
                    controlDB.Controls.DeleteOnSubmit(c1);
                }

                try
                {
                    controlDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                return true;
            }
        }

        //image related interface
        public static Image LoadImagesByPageId(String pageId)
        {
            using (ProtoDataContext imageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Image> images = from a in imageDB.Images
                                           where a.PageId == pageId
                                           select a;
                return images.ToList().FirstOrDefault();
            }
        }

        public static Image LoadImageByPK(String imageId)
        {
            using (ProtoDataContext imageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Image> image = from a in imageDB.Images
                                          where a.ImageId == imageId
                                          select a;
                return image.FirstOrDefault();
            }
        }

        public static bool SaveImage(Image i)
        {
            try
            {
                DeleteImage(i);
                AddImage(i);
            }
            catch
            {
                DeleteImage(i);
                AddImage(i);
                return false;
            }
            return true;
            //using (ProtoDataContext imageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            //{

            //    IQueryable<Image> image = from a in imageDB.Images
            //                              where a.ImageId == i.ImageId
            //                              select a;

            //    foreach (Image i1 in image)
            //    {
            //        i1.PageId = i.PageId;
            //        i1.ImageStatus = i.ImageStatus;
            //        i1.ImageSequence = i.ImageSequence;
            //        i1.ImageLastModifiedDate = DateTime.Now;
            //    }


            //    try
            //    {
            //        imageDB.SubmitChanges();
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }

            //    return true;
            //}
        }

        public static bool AddImage(Image i)
        {
            using (ProtoDataContext imageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {

                imageDB.Images.InsertOnSubmit(i);

                try
                {
                    imageDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    imageDB.SubmitChanges();
                }

                return true;
            }
        }

        //1. Assuming we delete the record from db directly rather than set the status of image to be deleted
        //2. The foreign key reference has not been figured out so the deletion of controls of this image haven't been deleted automatically, guess it would be done through FK reference
        public static bool DeleteImage(Image i)
        {
            using (ProtoDataContext imageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Image> image = from a in imageDB.Images
                                          where a.ImageId == i.ImageId
                                          select a;

                foreach (var i1 in image)
                {
                    imageDB.Images.DeleteOnSubmit(i1);
                }

                try
                {
                    imageDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                return true;
            }
        }

        //page related interface
        public static IList<Page> LoadPagesByProjectId(String projectId)
        {
            using (ProtoDataContext pageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Page> page = from a in pageDB.Pages
                                        where a.ProjectId == projectId
                                        select a;
                return page.ToList();
            }
        }
        public static Page LoadPageByPK(String pageId)
        {
            using (ProtoDataContext pageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Page> page = from a in pageDB.Pages
                                        where a.PageId == pageId
                                        select a;
                return page.FirstOrDefault();
            }
        }
        public static bool SavePage(Page p)
        {
            using (ProtoDataContext pageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {

                IQueryable<Page> page = from a in pageDB.Pages
                                        where a.PageId == p.PageId
                                        select a;

                foreach (Page p1 in page)
                {
                    p1.ProjectId = p.ProjectId;
                    p1.PageStatus = p.PageStatus;
                    p1.PageCapacity = p.PageCapacity;
                }

                try
                {
                    pageDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                return true;
            }
        }

        public static bool AddPage(Page p)
        {
            using (ProtoDataContext pageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {

                pageDB.Pages.InsertOnSubmit(p);

                try
                {
                    pageDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    pageDB.SubmitChanges();
                }

                return true;
            }
        }

        //1. Assuming we delete the record from db directly rather than set the status of page to be deleted
        //2. The foreign key reference has not been figured out so the deletion of images of this page haven't been deleted automatically, guess it would be done through FK reference
        public static bool DeletePage(Page p)
        {
            using (ProtoDataContext pageDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Page> page = from a in pageDB.Pages
                                        where a.PageId == p.PageId
                                        select a;

                foreach (var p1 in page)
                {
                    pageDB.Pages.DeleteOnSubmit(p1);
                }

                try
                {
                    pageDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                return true;
            }
        }

        //project related interface
        public static IList<Project> LoadAllProjects()
        {
            using (ProtoDataContext projectDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Project> projects = from a in projectDB.Projects
                                               select a;
                return projects.ToList();
            }
        }
        public static IList<Project> LoadProjectsByTag(String tag)
        {
            using (ProtoDataContext projectDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Project> projects = from a in projectDB.Projects
                                               where a.ProjectTag == tag
                                               select a;
                return projects.ToList();
            }
        }

        public static Project LoadProjectByPK(String projectId)
        {
            using (ProtoDataContext projectDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Project> project = from a in projectDB.Projects
                                              where a.ProjectId == projectId
                                              select a;
                return project.FirstOrDefault();
            }
        }

        public static bool SaveProject(Project p)
        {
            try
            {
                DeleteProject(p);
                AddProject(p);
            }
            catch (Exception) { return false; }
            return true;
        }

        public static bool AddProject(Project p)
        {
            using (ProtoDataContext projectDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {

                projectDB.Projects.InsertOnSubmit(p);

                try
                {
                    projectDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    projectDB.SubmitChanges();
                }

                return true;
            }
        }

        //1. Assuming we delete the record from db directly rather than set the status of project to be deleted
        //2. The foreign key reference has not been figured out so the deletion of pages of this project haven't been deleted automatically, guess it would be done through FK reference
        public static bool DeleteProject(Project p)
        {
            using (ProtoDataContext projectDB = new ProtoDataContext("isostore:/protoDataContext.sdf"))
            {
                IQueryable<Project> project = from a in projectDB.Projects
                                              where a.ProjectId == p.ProjectId
                                              select a;

                foreach (var p1 in project)
                {
                    projectDB.Projects.DeleteOnSubmit(p1);
                }

                try
                {
                    projectDB.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                return true;
            }
        }
        #endregion
    }



    #region table definations

    [Table]
    public class Project : INotifyPropertyChanged, INotifyPropertyChanging
    {

        //used
        public Project()
        {
            ProjectId = Guid.NewGuid().ToString();
            ProjectAuthor = 0;
            ProjectCreateDate = DateTime.Now;
            ProjectLastModifiedDate = DateTime.Now;
            ProjectName = "untitled";
            ProjectDescription = "no description";
            ProjectTag = "";
            ProjectStatus = (int)GlobalVariables.projectStatus.Independent;
            /* Not Sure Propoties */

        }

        public Project(String ProjectName, String ProjectTag, String ProjectThumb)
        {
            ProjectId = Guid.NewGuid().ToString();
            ProjectAuthor = 0;
            ProjectCreateDate = DateTime.Now;
            ProjectLastModifiedDate = DateTime.Now;
            this.ProjectName = ProjectName;
            this.ProjectThumb = "/ProtoImaginer;component/Images/ProjectThumbs/" + ProjectThumb;
            ProjectDescription = "no description";
            this.ProjectTag = ProjectTag;
            ProjectStatus = (int)GlobalVariables.projectStatus.Independent;
        }

        public Project(String ProjectId, int ProjectAuthor, String ProjectName, String ProjectDescription, String ProjectTag)
        {
            this.ProjectId = ProjectId;
            this.ProjectAuthor = ProjectAuthor;
            this.ProjectName = ProjectName;
            this.ProjectDescription = ProjectDescription;
            this.ProjectTag = ProjectTag;
            this.ProjectThumb = null;
            this.ProjectCreateDate = DateTime.Now;
            this.ProjectLastModifiedDate = DateTime.Now;
        }

        public Project(String ProjectId, int ProjectAuthor, String ProjectName, int ProjectStatus,
            String ProjectDescription, String ProjectTag, DateTime ProjectCreateDate, DateTime ProjectLastModifiedDate)
        {
            this.ProjectId = ProjectId;
            this.ProjectAuthor = ProjectAuthor;
            this.ProjectName = ProjectName;
            this.ProjectStatus = ProjectStatus;
            this.ProjectDescription = ProjectDescription;
            this.ProjectThumb = null;
            this.ProjectTag = ProjectTag;
            this.ProjectCreateDate = ProjectCreateDate;
            this.ProjectLastModifiedDate = ProjectLastModifiedDate;
            this.ProjectThumb = "/ProtoImaginer;component/Images/ProjectThumbs/-1.jpg";
        }

        public Project(String ProjectId, int ProjectAuthor, String ProjectName, int ProjectStatus, String ProjectDescription, 
            String ProjectTag, DateTime ProjectCreateDate, DateTime ProjectLastModifiedDate, string ProjectThumb)
        {
            this.ProjectId = ProjectId;
            this.ProjectAuthor = ProjectAuthor;
            this.ProjectName = ProjectName;
            this.ProjectStatus = ProjectStatus;
            this.ProjectDescription = ProjectDescription;
            this.ProjectThumb = null;
            this.ProjectTag = ProjectTag;
            this.ProjectCreateDate = ProjectCreateDate;
            this.ProjectLastModifiedDate = ProjectLastModifiedDate;
            this.ProjectThumb = (this.ProjectThumb == null) ? 
                "/ProtoImaginer;component/Images/ProjectThumbs/-1.jpg" 
                : ProjectThumb;
        }
        // Define ID: private field, public property, and database column.
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public String ProjectId
        {
            get;
            set;
        }

        // Define item name: private field, public property, and database column.
        [Column(CanBeNull = false)]
        public String ProjectName
        {

            get;
            set;
        }

        //Define the description
        [Column]
        public String ProjectDescription
        {

            get;
            set;
        }

        //Define the keywords
        [Column]
        public String ProjectTag
        {

            get;
            set;
        }

        //Define the thumb
        [Column]
        public String ProjectThumb
        {

            get;
            set;
        }

        //Define the Author
        [Column]
        public int ProjectAuthor
        {

            get;
            set;
        }

        //Define the Project CreateDate
        [Column]
        public DateTime ProjectCreateDate
        {

            get;
            set;
        }

        //Define the Project LastModifiedDate
        [Column]
        public DateTime ProjectLastModifiedDate
        {

            get;
            set;
        }

        //Define the project status
        [Column]
        public int ProjectStatus
        {

            get;
            set;
        }

        // Version column aids update performance.
        //[Column(IsVersion = true)]
        //private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(String propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

    }

    [Table]
    public class Page : INotifyPropertyChanged, INotifyPropertyChanging
    {
        //used
        public Page()
        {
            PageId = Guid.NewGuid().ToString();
            this.ProjectId = null;
            this.PageTitle = "Untitled";
            this.PageStatus = (int)GlobalVariables.pageStatus.Independent;
            //A page contains at most 7 images.
            //PageCapacity decreases by 1 once a new image created under this page. 
            //A new page has to be created once PageCapacity reaches 0.
            this.PageCapacity = GlobalVariables.maximumPageCapacity;
        }

        //used
        public Page(String projectId)
        {
            PageId = Guid.NewGuid().ToString();
            this.ProjectId = projectId;
            this.PageTitle = "Untitled";
            this.PageStatus = (int)GlobalVariables.pageStatus.Independent;
            //A page contains at most 7 images.
            //PageCapacity decreases by 1 once a new image created under this page. 
            //A new page has to be created once PageCapacity reaches 0.
            this.PageCapacity = GlobalVariables.maximumPageCapacity;
        }


        //used
        public Page(String projectId, String pageName)
        {
            PageId = Guid.NewGuid().ToString();
            this.ProjectId = projectId;
            this.PageTitle = pageName;
            this.PageStatus = (int)GlobalVariables.pageStatus.Independent;
            //A page contains at most 7 images.
            //PageCapacity decreases by 1 once a new image created under this page. 
            //A new page has to be created once PageCapacity reaches 0.
            this.PageCapacity = GlobalVariables.maximumPageCapacity;
        }

        public Page(String pageId, String projectId, int pageStatus)
        {
            this.PageId = pageId;
            this.ProjectId = projectId;
            this.PageStatus = pageStatus;
            //A page contains at most 7 images. 
            //PageCapacity decreases by 1 once a new image created under this page. 
            //A new page has to be created once PageCapacity reaches 0.
            this.PageCapacity = GlobalVariables.maximumPageCapacity;
        }

        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public String PageId
        {

            get;
            set;
        }

        [Column]
        public String PageTitle
        {
            get;
            set;
        }

        //Foreign key
        [Column(CanBeNull = false)]
        public String ProjectId
        {
            get;
            set;
        }

        [Column]
        public int PageStatus
        {
            get;
            set;
        }

        //A page contains at most 7 images.
        [Column]
        public int PageCapacity
        {
            get;
            set;
        }

        // Version column aids update performance.
        //[Column(IsVersion = true)]
        //private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(String propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

    [Table]
    public class Image : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public Image()
        {
            ImageId = Guid.NewGuid().ToString();
            ImageSequence = -1;
            PageId = null;
            ImageSource = " ";
            ImageThumb = " ";
            ImageCreateDate = DateTime.Now;
            ImageLastModifiedDate = DateTime.Now;
            ImageStatus = (int)GlobalVariables.imageStatus.Independent;
        }
        //used
        public Image(String pageId, String imageSource, String imageThumb)
        {
            ImageId = Guid.NewGuid().ToString();
            ImageSequence = -1;
            PageId = pageId;
            ImageSource = "/ProtoImaginer;component/Images/PageFullSize/" + imageSource;
            ImageThumb = "/ProtoImaginer;component/Images/PageThumbs/" + imageThumb;
            ImageCreateDate = DateTime.Now;
            ImageLastModifiedDate = DateTime.Now;
            ImageStatus = (int)GlobalVariables.imageStatus.Independent;

        }
        public Image(String imageId, int imageSequence, DateTime imageCreateDate, DateTime imageLastModifiedDate, int imageStatus)
        {
            this.ImageId = imageId;
            this.ImageSequence = imageSequence;
            this.ImageCreateDate = imageCreateDate;
            ImageSource = " ";
            ImageThumb = " ";
            this.ImageLastModifiedDate = imageLastModifiedDate;
            this.ImageStatus = imageStatus;
        }

        // Define ID: private field, public property, and database column.

        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public String ImageId
        {

            get;
            set;
        }

        //Foreign key
        [Column]
        public String PageId
        {

            get;
            set;
        }

        //let's say the sequence id assigned to this page within the project, e.g. 1, 2, 3
        [Column]
        public int ImageSequence
        {
            get;
            set;
        }

        [Column]
        public String ImageSource
        {
            get;
            set;
        }

        [Column]
        public String ImageThumb
        {
            get;
            set;
        }

        [Column]
        public DateTime ImageCreateDate
        {
            get;
            set;
        }

        [Column]
        public DateTime ImageLastModifiedDate
        {
            get;
            set;
        }

        [Column]
        public int ImageStatus
        {
            get;
            set;
        }

        // Version column aids update performance.
        //[Column(IsVersion = true)]
        //private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(String propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

    [Table]
    public class Control : INotifyPropertyChanged, INotifyPropertyChanging
    {
        //shouldn't be used
        public Control()
        {
            ControlId = Guid.NewGuid().ToString();
            Control_Top_Left_Point_X_Coordinate = 0;
            Control_Top_Left_Point_Y_Coordinate = 0;
            Control_Width = 100;
            Control_Height = 75;
            ControlStatus = (int)GlobalVariables.controlStatus.Independent;
            ControlCreateDate = DateTime.Now;
        }

        //used
        public Control(String residingPageId)
        {
            ControlId = Guid.NewGuid().ToString();
            Control_Top_Left_Point_X_Coordinate = 0;
            Control_Top_Left_Point_Y_Coordinate = 0;
            Control_Width = 100;
            Control_Height = 75;
            this.ControlResidingImageId = residingPageId;
            ControlStatus = (int)GlobalVariables.controlStatus.Independent;
            ControlCreateDate = DateTime.Now;
        }

        //used
        public Control(String residingPageId, double top_left_x, double top_left_y)
        {
            ControlId = Guid.NewGuid().ToString();
            Control_Top_Left_Point_X_Coordinate = top_left_x;
            Control_Top_Left_Point_Y_Coordinate = top_left_y;
            Control_Width = 100;
            Control_Height = 75;
            this.ControlResidingImageId = residingPageId;
            ControlStatus = (int)GlobalVariables.controlStatus.Independent;
            ControlCreateDate = DateTime.Now;
        }
        //used
        public Control(String residingPageId, double top_left_x, double top_left_y, String linkingPageId)
        {
            ControlId = Guid.NewGuid().ToString();
            Control_Top_Left_Point_X_Coordinate = top_left_x;
            Control_Top_Left_Point_Y_Coordinate = top_left_y;
            Control_Width = 100;
            Control_Height = 75;
            this.ControlResidingImageId = residingPageId;
            this.ControlLinkToImageId = linkingPageId;
            ControlStatus = (int)GlobalVariables.controlStatus.Independent;
            ControlCreateDate = DateTime.Now;
        }
        //used
        public Control(String residingPageId, double top_left_x, double top_left_y, int width, int height)
        {
            ControlId = Guid.NewGuid().ToString();
            Control_Top_Left_Point_X_Coordinate = top_left_x;
            Control_Top_Left_Point_Y_Coordinate = top_left_y;
            Control_Width = width;
            Control_Height = height;
            this.ControlResidingImageId = residingPageId;
            ControlStatus = (int)GlobalVariables.controlStatus.Independent;
            ControlCreateDate = DateTime.Now;
        }
        //used
        public Control(String residingPageId, double top_left_x, double top_left_y, int width, int height, String linkingPageId)
        {
            ControlId = Guid.NewGuid().ToString();
            Control_Top_Left_Point_X_Coordinate = top_left_x;
            Control_Top_Left_Point_Y_Coordinate = top_left_y;
            Control_Width = width;
            Control_Height = height;
            this.ControlResidingImageId = residingPageId;
            this.ControlLinkToImageId = linkingPageId;
            ControlStatus = (int)GlobalVariables.controlStatus.Independent;
            ControlCreateDate = DateTime.Now;
        }
        public Control(String controlId, double top_left_x, double top_left_y,
            int width, int height, String residingPageId, String linkToPageId, int status, DateTime createDate)
        {
            this.ControlId = controlId;
            this.Control_Top_Left_Point_X_Coordinate = top_left_x;
            this.Control_Top_Left_Point_Y_Coordinate = top_left_y;
            this.Control_Width = width;
            this.Control_Height = height;
            this.ControlResidingImageId = residingPageId;
            this.ControlLinkToImageId = linkToPageId;
            this.ControlStatus = status;
            this.ControlCreateDate = createDate;
        }

        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public String ControlId
        {

            get;
            set;
        }

        [Column(CanBeNull = false)]
        public double Control_Top_Left_Point_X_Coordinate
        {
            get;
            set;
        }

        [Column(CanBeNull = false)]
        public double Control_Top_Left_Point_Y_Coordinate
        {
            get;
            set;
        }

        [Column(CanBeNull = false)]
        public int Control_Width
        {
            get;
            set;
        }

        [Column(CanBeNull = false)]
        public int Control_Height
        {
            get;
            set;
        }

        //Foreign key
        [Column(CanBeNull = false)]
        public String ControlResidingImageId
        {

            get;
            set;
        }

        //Foreign key
        [Column]
        public String ControlLinkToImageId
        {
            get;
            set;
        }

        [Column]
        public int ControlStatus
        {
            get;
            set;
        }

        [Column]
        public DateTime ControlCreateDate
        {
            get;
            set;
        }

        // Version column aids update performance.
        //[Column(IsVersion = true)]
        //private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(String propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

    //[Table] unused page class
    //public class Page : INotifyPropertyChanged, INotifyPropertyChanging
    //{
    //    public Page() 
    //    {
    //        Guid id = new Guid();
    //        PageId = id.ToString();
    //        PageTitle = "untitled";
    //        PageAuthor = 0;
    //        PageStatus = 0;
    //        PageCreateDate = DateTime.Now;
    //        PageLastModifiedDate = DateTime.Now;
    //    }
    //    public Page(String PageId, String PageTitle, int PageAuthor, Guid PageProjectId,
    //        DateTime PageCreateDate, DateTime PageLastModifiedDate, int PageStatus)
    //    {
    //        this.PageId = PageId;
    //        this.PageTitle = PageTitle;
    //        this.PageAuthor = PageAuthor;
    //        this.PageStatus = PageStatus;
    //        this.PageCreateDate = PageCreateDate;
    //        this.PageLastModifiedDate = PageLastModifiedDate;
    //    }

    //    // Define ID: private field, public property, and database column.

    //    [Column(IsPrimaryKey = true, CanBeNull = false)]
    //    public String PageId
    //    {

    //        get;
    //        set;
    //    }

    //    // Define category name: private field, public property, and database column.

    //    [Column]
    //    public String PageTitle
    //    {

    //        get;
    //        set;
    //    }

    //    //Define the Author

    //    [Column]
    //    public int PageAuthor
    //    {

    //        get;
    //        set;
    //    }


    //    [Column]
    //    public String PageProjectId
    //    {

    //        get;
    //        set;
    //    }

    //    //Define the Project CreateDate

    //    [Column]
    //    public DateTime PageCreateDate
    //    {

    //        get;
    //        set;
    //    }

    //    //Define the Project LastModifiedDate

    //    [Column]
    //    public DateTime PageLastModifiedDate
    //    {

    //        get;
    //        set;
    //    }

    //    //Define the project status

    //    [Column]
    //    public int PageStatus
    //    {

    //        get;
    //        set;
    //    }


    //    // Version column aids update performance.
    //    //[Column(IsVersion = true)]
    //    //private Binary _version;

    //    #region INotifyPropertyChanged Members

    //    public event PropertyChangedEventHandler PropertyChanged;

    //    // Used to notify that a property changed
    //    private void NotifyPropertyChanged(String propertyName)
    //    {
    //        if (PropertyChanged != null)
    //        {
    //            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    //        }
    //    }

    //    #endregion

    //    #region INotifyPropertyChanging Members

    //    public event PropertyChangingEventHandler PropertyChanging;

    //    // Used to notify that a property is about to change
    //    private void NotifyPropertyChanging(String propertyName)
    //    {
    //        if (PropertyChanging != null)
    //        {
    //            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
    //        }
    //    }

    //    #endregion
    //}

    //[Table] unused relations
    //public class Image_Contain_Control : INotifyPropertyChanged, INotifyPropertyChanging
    //{

    //    public Image_Contain_Control(String controlId, String imageId)
    //    {
    //        this.ControlId = controlId;
    //        this.ImageId = imageId;
    //    }

    //    //A unique control can only be contained by exactly 1 image.
    //    //A image can contain * controls
    //    [Column(IsPrimaryKey = true, CanBeNull = false)] 
    //    public String ControlId
    //    {

    //        get;
    //        set;
    //    }

    //    //Foreign key
    //    [Column(CanBeNull = false)]
    //    public String ImageId
    //    {
    //        get;
    //        set;
    //    }

    //    // Version column aids update performance.
    //    //[Column(IsVersion = true)]
    //    //private Binary _version;

    //    #region INotifyPropertyChanged Members

    //    public event PropertyChangedEventHandler PropertyChanged;

    //    // Used to notify that a property changed
    //    private void NotifyPropertyChanged(String propertyName)
    //    {
    //        if (PropertyChanged != null)
    //        {
    //            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    //        }
    //    }

    //    #endregion

    //    #region INotifyPropertyChanging Members

    //    public event PropertyChangingEventHandler PropertyChanging;

    //    // Used to notify that a property is about to change
    //    private void NotifyPropertyChanging(String propertyName)
    //    {
    //        if (PropertyChanging != null)
    //        {
    //            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
    //        }
    //    }

    //    #endregion
    //}

    //[Table]
    //public class Page_Contain_Image : INotifyPropertyChanged, INotifyPropertyChanging
    //{

    //    public Page_Contain_Image(String imageId, String pageId)
    //    {
    //        this.ImageId = imageId;
    //        this.PageId = pageId;
    //    }

    //    //Assume a unique image object (not the picture) can only be contained by exactly 1 page.
    //    //A page can contain up to 7 images.
    //    [Column(IsPrimaryKey = true, CanBeNull = false)]
    //    public String ImageId
    //    {

    //        get;
    //        set;
    //    }

    //    //Foreign key
    //    [Column(CanBeNull = false)]
    //    public String PageId
    //    {
    //        get;
    //        set;
    //    }

    //    // Version column aids update performance.
    //    //[Column(IsVersion = true)]
    //    //private Binary _version;

    //    #region INotifyPropertyChanged Members

    //    public event PropertyChangedEventHandler PropertyChanged;

    //    // Used to notify that a property changed
    //    private void NotifyPropertyChanged(String propertyName)
    //    {
    //        if (PropertyChanged != null)
    //        {
    //            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    //        }
    //    }

    //    #endregion

    //    #region INotifyPropertyChanging Members

    //    public event PropertyChangingEventHandler PropertyChanging;

    //    // Used to notify that a property is about to change
    //    private void NotifyPropertyChanging(String propertyName)
    //    {
    //        if (PropertyChanging != null)
    //        {
    //            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
    //        }
    //    }

    //    #endregion
    //}

    #endregion
}
